package org.dong.samples.service;

import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Workbook;
import org.dong.samples.dao.UserHibernateDAO;
import org.dong.samples.model.User;
import org.dong.samples.model.vo.UserVO;
import org.dong.samples.util.ExcelUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ManageStudentService  {
	
	@Autowired
	private UserHibernateDAO userHiberanteDAO;
	
	public void saveStudentByExcel(InputStream inputStream, String pos) throws ParseException {
		Workbook wk = ExcelUtils.openWorkbook(inputStream, pos);
		List<UserVO> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		List<User> userList = new ArrayList<User>();
		if(allExcelUser != null){
//			Role role = examRoleDAO.get(25);
			User user = null;
			for(int i = 0; i < allExcelUser.size(); i++){
				user = new User();
				BeanUtils.copyProperties(allExcelUser.get(i), user);
//				Set<Role> examRoles = new HashSet<Role>();
//				examRoles.add(role);
//				user.setExamRoles(examRoles);
				userList.add(user);
			}
		}
		userHiberanteDAO.saveOrUpdate(userList);
	}
	
}
